// Copyright (c) 2012, yuno. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
package sugarmintcandy.widgetwindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class VerticalAlphaBar extends View {
    private OnChangeListener mOnChangeListener;
    private float[] mHSV;
    private float mAlpha;
    private Paint mPaint1;
    private Paint mPaint2;
    private Paint mPaint3;
    private Paint mPaint4;
    private Paint mPaint5;

    public interface OnChangeListener {
	void onChange(VerticalAlphaBar view);
    }

    public VerticalAlphaBar(Context context, AttributeSet attrs) {
	super(context, attrs);
	mHSV = new float[3];
	Color.colorToHSV(-1, mHSV);
	mAlpha = 1;
    }

    public void setOnChangeListener(OnChangeListener l) {
	mOnChangeListener = l;
    }

    public float[] getHSV() {
	return mHSV.clone();
    }

    public float getAlphaValue() {
	return mAlpha;
    }

    public void setHSV(float[] hsv) {
	mHSV[0] = hsv[0];
	mHSV[1] = hsv[1];
	mHSV[2] = hsv[2];
	mPaint2 = null;
	invalidate();
    }

    public void setAlphaValue(float alpha) {
	mAlpha = alpha;
	invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
	    int bottom) {
	super.onLayout(changed, left, top, right, bottom);
	if (changed) {
	    mPaint2 = null;
	}
    }

    @Override
    protected void onDraw(Canvas canvas) {
	super.onDraw(canvas);

	int left = getPaddingLeft() + 4;
	int top = getPaddingTop() + 4;
	int right = getWidth() - getPaddingRight() - 4;
	int bottom = getHeight() - getPaddingBottom() - 4;

	if (mPaint1 == null) {
	    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
		    R.drawable.checker);
	    Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT,
		    Shader.TileMode.REPEAT);
	    mPaint1 = new Paint();
	    mPaint1.setShader(shader);
	}
	canvas.drawRect(left - 2, top - 2, right + 2, bottom + 2, mPaint1);

	if (mPaint2 == null) {
	    int topColor = Color.HSVToColor(255, mHSV);
	    int bottomColor = Color.HSVToColor(0, mHSV);
	    Shader shader = new LinearGradient(left, top, left, bottom,
		    topColor, bottomColor, Shader.TileMode.CLAMP);
	    mPaint2 = new Paint();
	    mPaint2.setShader(shader);
	}
	canvas.drawRect(left - 2, top - 2, right + 2, bottom + 2, mPaint2);

	if (mPaint3 == null) {
	    mPaint3 = new Paint();
	    mPaint3.setAntiAlias(true);
	    mPaint3.setColor(0xff0080ff);
	    mPaint3.setStyle(Paint.Style.STROKE);
	    mPaint3.setStrokeWidth(4);
	}
	canvas.drawRect(left - 2, top - 2, right + 2, bottom + 2, mPaint3);

	float py = (1 - mAlpha) * (bottom - top) + top;

	if (mPaint4 == null) {
	    mPaint4 = new Paint();
	    mPaint4.setAntiAlias(true);
	    mPaint4.setColor(0xff000000);
	    mPaint4.setStyle(Paint.Style.STROKE);
	    mPaint4.setStrokeWidth(3);
	}
	canvas.drawRect(left, py - 4, right, py + 4, mPaint4);

	if (mPaint5 == null) {
	    mPaint5 = new Paint();
	    mPaint5.setAntiAlias(true);
	    mPaint5.setColor(0xffffffff);
	    mPaint5.setStyle(Paint.Style.STROKE);
	    mPaint5.setStrokeWidth(2);
	}
	canvas.drawRect(left, py - 4, right, py + 4, mPaint5);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
	int action = event.getAction();
	if (action == MotionEvent.ACTION_DOWN
		|| action == MotionEvent.ACTION_MOVE) {
	    float y = event.getY();
	    int top = getPaddingTop() + 4;
	    int bottom = getHeight() - getPaddingBottom() - 4;

	    float a = (y - bottom) / (top - bottom);
	    mAlpha = Math.max(0, Math.min(1, a));
	    invalidate();
	    if (mOnChangeListener != null) {
		mOnChangeListener.onChange(this);
	    }
	}
	return true;
    }
}
